DECLARE @tbl AS TABLE ( num INT )
INSERT INTO @tbl
  (
    num
  )
SELECT entityid
FROM   VmEntity ve
ORDER BY
       ve.EntityID

DECLARE @i AS INT, @j INT
SET @i = RAND() * 9
SET @j = RAND() * 30
DELETE 
FROM   @tbl
WHERE  num BETWEEN @i AND @j
PRINT @i
PRINT @j
SET @i = RAND() * 50
PRINT @i
DELETE 
FROM   @tbl
WHERE  num = @i

SELECT t1.num +1 , (SELECT min(t.num)-1 FROM @tbl t WHERE t.num>t1.NUM)
FROM   @tbl t1
       LEFT OUTER JOIN @tbl t2
            ON  t2.num = t1.num + 1 
WHERE  t2.num IS NULL  


SELECT *
FROM   @tbl